<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 7.1.1">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/avatar.jpg">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/avatar.jpg">
  <link rel="mask-icon" href="/images/avatar.jpg" color="#222">
  <meta name="msvalidate.01" content="4AF2328B2035748153A041B7EEF3D15C">

<link rel="stylesheet" href="/css/main.css">

<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Noto+Serif+SC:300,300italic,400,400italic,700,700italic%7CLobster:300,300italic,400,400italic,700,700italic%7CPingFang+SC:300,300italic,400,400italic,700,700italic%7CRoboto+Mono:300,300italic,400,400italic,700,700italic&display=swap&subset=latin,latin-ext">

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css" integrity="sha256-wiz7ZSCn/btzhjKDQBms9Hx4sSeUYsDrTLg7roPstac=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.css" integrity="sha256-gkQVf8UKZgQ0HyuxL/VnacadJ+D2Kox2TCEBuNQg5+w=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"ethanliu6.github.io","root":"/","images":"/images","scheme":"Gemini","darkmode":false,"version":"8.19.2","exturl":true,"sidebar":{"position":"left","display":"always","padding":12,"offset":18},"copycode":{"enable":true,"style":"mac"},"fold":{"enable":true,"height":300},"bookmark":{"enable":true,"color":"#222","save":"manual"},"mediumzoom":true,"lazyload":true,"pangu":false,"comments":{"style":"buttons","active":"changyan","storage":true,"lazyload":false,"nav":null,"activeClass":"changyan"},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/search.xml","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":6,"unescape":false,"preload":false}}</script><script src="/js/config.js"></script>

    <meta name="description" content="JavaSE基础——Day08 该篇主要讲解Java中的数组">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaSE基础——Day08 JavaSE基础——数组">
<meta property="og:url" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/index.html">
<meta property="og:site_name" content="&lt;font color&#x3D;&#39;white&#39; &gt;Ethan の Blog&lt;&#x2F;font&gt;">
<meta property="og:description" content="JavaSE基础——Day08 该篇主要讲解Java中的数组">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640178676906.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640178695398.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640179442957.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640179577860.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640179610351.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640179641419.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640179738721.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640180154556.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640180271885.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640180296416.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640180317142.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640180362775.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640180957760.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640181579146.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640181720120.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640181750266.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640181788943.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640181816425.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640182140083.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640182579200.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640182850919.png">
<meta property="og:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640183100572.png">
<meta property="article:published_time" content="2024-03-21T16:00:00.000Z">
<meta property="article:modified_time" content="2024-04-26T05:13:12.339Z">
<meta property="article:author" content="Ethan Liu">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="Java基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/imgs/1640178676906.png">


<link rel="canonical" href="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/","path":"2024/03/22/JavaNotes/JavaSE基础/day08数组/","title":"JavaSE基础——Day08 JavaSE基础——数组"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JavaSE基础——Day08 JavaSE基础——数组 | <font color='white' >Ethan の Blog</font></title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>
<style>.darkmode--activated{--body-bg-color:#282828;--content-bg-color:#333;--card-bg-color:#555;--text-color:#ccc;--blockquote-color:#bbb;--link-color:#ccc;--link-hover-color:#eee;--brand-color:#ddd;--brand-hover-color:#ddd;--table-row-odd-bg-color:#282828;--table-row-hover-bg-color:#363636;--menu-item-bg-color:#555;--btn-default-bg:#222;--btn-default-color:#ccc;--btn-default-border-color:#555;--btn-default-hover-bg:#666;--btn-default-hover-color:#ccc;--btn-default-hover-border-color:#666;--highlight-background:#282b2e;--highlight-foreground:#a9b7c6;--highlight-gutter-background:#34393d;--highlight-gutter-foreground:#9ca9b6}.darkmode--activated img{opacity:.75}.darkmode--activated img:hover{opacity:.9}.darkmode--activated code{color:#69dbdc;background:0 0}button.darkmode-toggle{z-index:9999}.darkmode-ignore,img{display:flex!important}.beian img{display:inline-block!important}</style></head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title"><font color='white' >Ethan の Blog</font></p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description"><font color='#FDABC1' size=3>清醒的停留，胜过——盲目的前行</font></p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-关于我"><a href="/aboutme/" rel="section"><i class="fa fa-user fa-fw"></i>关于我</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签<span class="badge">18</span></a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类<span class="badge">14</span></a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档<span class="badge">53</span></a></li><li class="menu-item menu-item-love"><a href="/categories/Love/" rel="section"><i class="fas fa-heart fa-fw"></i>Love</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%80%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-text">1：一维数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E6%A6%82%E8%BF%B0%E5%92%8C%E8%AF%BE%E7%A8%8B%E5%AE%89%E6%8E%92"><span class="nav-text">1.1 数组概述和课程安排</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B1%E6%95%B0%E7%BB%84%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C%E4%B9%8B%E9%81%8D%E5%8E%86"><span class="nav-text">1.4 案例1(数组常见操作之遍历)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B2%E6%95%B0%E7%BB%84%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C%E4%B9%8B%E8%8E%B7%E5%8F%96%E6%9C%80%E5%A4%A7%E5%80%BC"><span class="nav-text">1.5
案例2(数组常见操作之获取最大值)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B3%E6%95%B0%E7%BB%84%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C%E4%B9%8B%E5%85%83%E7%B4%A0%E6%89%93%E4%B9%B1"><span class="nav-text">1.6
案例3(数组常见操作之元素打乱)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E5%8A%A8%E6%80%81%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">1.7 数组动态初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B4%E6%95%B0%E7%BB%84%E5%85%83%E6%9D%A5%E8%87%AA%E9%94%AE%E7%9B%98%E5%BD%95%E5%85%A5"><span class="nav-text">1.8 案例4(数组元来自键盘录入)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E5%86%85%E5%AD%98%E5%9B%BE"><span class="nav-text">1.9 数组内存图</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%95%B0%E7%BB%84%E4%BD%BF%E7%94%A8%E4%B8%AD%E7%9A%84%E4%B8%A4%E4%B8%AA%E5%B0%8F%E9%97%AE%E9%A2%98"><span class="nav-text">1.10 数组使用中的两个小问题</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84"><span class="nav-text">2：二维数组</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E6%A6%82%E8%BF%B0%E5%92%8C%E8%AF%BE%E7%A8%8B%E5%AE%89%E6%8E%92"><span class="nav-text">2.1 二维数组概述和课程安排</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E5%88%9D%E5%A7%8B%E5%8C%96"><span class="nav-text">2.2 二维数组初始化</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E5%85%83%E7%B4%A0%E8%AE%BF%E9%97%AE"><span class="nav-text">2.3 二维数组元素访问</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E5%86%85%E5%AD%98%E5%9B%BE"><span class="nav-text">2.4 二维数组内存图</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B5%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C%E4%B9%8B%E9%81%8D%E5%8E%86"><span class="nav-text">2.5
案例5(二维数组常见操作之遍历)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B6%E4%BA%8C%E7%BB%B4%E6%95%B0%E7%BB%84%E5%B8%B8%E8%A7%81%E6%93%8D%E4%BD%9C%E4%B9%8B%E5%85%83%E7%B4%A0%E6%89%93%E4%B9%B1"><span class="nav-text">2.6
案例6(二维数组常见操作之元素打乱)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B7%E5%9B%BE%E7%89%87%E5%B1%95%E7%A4%BA"><span class="nav-text">2.7 案例7(图片展示)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%A1%88%E4%BE%8B8%E5%9B%BE%E7%89%87%E6%89%93%E4%B9%B1"><span class="nav-text">2.8 案例8(图片打乱)</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="Ethan Liu"
      src="/images/avatar.jpg">
  <p class="site-author-name" itemprop="name">Ethan Liu</p>
  <div class="site-description" itemprop="description"><font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font></div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">53</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">14</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">18</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author animated">
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9naXRodWIuY29tL2V0aGFubGl1Ng==" title="GitHub → https:&#x2F;&#x2F;github.com&#x2F;ethanliu6"><i class="fab fa-github fa-fw"></i>GitHub</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="bWFpbHRvOmxpdWUyMzM2QGdtYWlsLmNvbQ==" title="E-Mail → mailto:liue2336@gmail.com"><i class="fa fa-envelope fa-fw"></i>E-Mail</span>
      </span>
      <span class="links-of-author-item">
        <span class="exturl" data-url="aHR0cHM6Ly9zcGFjZS5iaWxpYmlsaS5jb20vMTMyNzA5OTk3Nw==" title="BiliBili → https:&#x2F;&#x2F;space.bilibili.com&#x2F;1327099977"><i class="fa-brands fa-bilibili fa-fw"></i>BiliBili</span>
      </span>
      <span class="links-of-author-item">
        <a href="/categories/Love/" title="Love → &#x2F;categories&#x2F;Love&#x2F;" rel="noopener me"><i class="fa fa-heart fa-fw"></i>Love</a>
      </span>
  </div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.jpg">
      <meta itemprop="name" content="Ethan Liu">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="<font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="<font color='#0000FF' size=1.5px>语言学 | NLP | Py | Java</font>">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="JavaSE基础——Day08 JavaSE基础——数组 | <font color='white' >Ethan の Blog</font>">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaSE基础——Day08 JavaSE基础——数组<span class="exturl post-edit-link" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni9ldGhhbmxpdTYuZ2l0aHViLmlvL3RyZWUvbWFzdGVyL19wb3N0cy9KYXZhTm90ZXMvSmF2YVNF5Z+656GAL2RheTA45pWw57uELm1k" title="编辑"><i class="fa fa-pen-nib"></i></span>
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2024-03-22 00:00:00" itemprop="dateCreated datePublished" datetime="2024-03-22T00:00:00+08:00">2024-03-22</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar-check"></i>
      </span>
      <span class="post-meta-item-text">更新于</span>
      <time title="修改时间：2024-04-26 13:13:12" itemprop="dateModified" datetime="2024-04-26T13:13:12+08:00">2024-04-26</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/Java%E5%85%A8%E6%A0%88/" itemprop="url" rel="index"><span itemprop="name">Java全栈</span></a>
        </span>
    </span>

  
    <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv">
      <span class="post-meta-item-icon">
        <i class="far fa-eye"></i>
      </span>
      <span class="post-meta-item-text">阅读次数：</span>
      <span id="busuanzi_value_page_pv"></span>
    </span>
  
  <span class="post-meta-item">
    
      <span class="post-meta-item-icon">
        <i class="far fa-comment"></i>
      </span>
      <span class="post-meta-item-text">Changyan：</span>
    
    <a title="JavaSE基础——Day08 JavaSE基础——数组" href="/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/#SOHUCS" itemprop="discussionUrl">
      <span id="sourceId::1ebf8e539b79390756bba9475ddb75d7" class="cy_cmt_count" itemprop="commentCount"></span>
    </a>
  </span>
  
  
    <span class="post-meta-break"></span>
    <span class="post-meta-item" title="本文字数">
      <span class="post-meta-item-icon">
        <i class="far fa-file-word"></i>
      </span>
      <span class="post-meta-item-text">本文字数：</span>
      <span>5k</span>
    </span>
    <span class="post-meta-item" title="阅读时长">
      <span class="post-meta-item-icon">
        <i class="far fa-clock"></i>
      </span>
      <span class="post-meta-item-text">阅读时长 &asymp;</span>
      <span>18 分钟</span>
    </span>
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody"><blockquote class="blockquote-center">
<p>JavaSE基础——Day08 该篇主要讲解Java中的<strong>数组</strong></p>

</blockquote>
<span id="more"></span>
<p><strong>今日目标：</strong></p>
<ul>
<li>能够知道数组的作用</li>
<li>能够根据需求完成数组的定义和初始化</li>
<li>能够使用数组完成授课案例</li>
<li>能够理解数组内存图</li>
</ul>
<h2 id="一维数组">1：一维数组</h2>
<h3 id="数组概述和课程安排">1.1 数组概述和课程安排</h3>
<p>来，继续啊，下面我们来学习数组。</p>
<p>那什么是数组呢？为了帮助大家理解，这里我们来看这样的一个场景：</p>
<figure>
<img data-src="imgs/1640178676906.png" alt="1640178676906">
<figcaption aria-hidden="true">1640178676906</figcaption>
</figure>
<p>看这里，有一个学员的考试成绩，为了存储这个学员的考试成绩，我们可以采用一个变量进行存储。</p>
<p>但是，假如这里有很多个学员的考试成绩，我们要存储该怎么办呢？</p>
<figure>
<img data-src="imgs/1640178695398.png" alt="1640178695398">
<figcaption aria-hidden="true">1640178695398</figcaption>
</figure>
<p>估计有同学就想到了，采用多个变量存储不就行了吗。对，想的没错。</p>
<p>确实可以采用多个变量进行存储，只不过将来要对这多个变量进行操作就比较麻烦了，</p>
<p>比如说：找到成绩最高的学员。按照我们学过的获取两个数的较大值，需要编写大量的判断逻辑才能完成这个操作。</p>
<p>很明显，采用多个变量存储不是好的解决方案。</p>
<p>那怎么办呢？我们再来回顾一下，目前出现的问题是：</p>
<p>1：一次性声明大量的用于存储数据的变量，</p>
<p>2：要存储的数据通常都是同类型数据，例如：考试成绩。</p>
<p>为了解决这种大量的同类型的数据存储，Java就提供了数组供我们使用。</p>
<p>比如说：下面这种格式，就是定义了一个存储多个考试成绩的数组。</p>
<ul>
<li>``` int[] scores = {100,100,100,100,100,100,100…};
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br></pre></td><td class="code"><pre><span class="line"></span><br><span class="line">  </span><br><span class="line"></span><br><span class="line">对比一下，比定义多个变量要方便多了，而且操作起来也是很方便的，至于怎么操作，一会我们详细讲解。</span><br><span class="line"></span><br><span class="line">好了，知道了数组可以解决这种问题后，我们给数组下一个定义：</span><br><span class="line"></span><br><span class="line">- **数组(array)：**是一种用于存储&lt;font color=&#x27;red&#x27;&gt;**多个相同数据类型**&lt;/font&gt;的存储模型(可以理解为容器)。</span><br><span class="line"></span><br><span class="line">知道了什么是数组后，我们来说一下数组这一块的课程安排：</span><br><span class="line"></span><br><span class="line">![1640178795993](imgs/1640178795993.png)</span><br><span class="line"></span><br><span class="line">首先，我们讲解数组的定义格式，这样我们就能够定义数组了。</span><br><span class="line"></span><br><span class="line">接着，我们讲解数组的初始化，这样我们就能够给数组中的元素赋值了。</span><br><span class="line"></span><br><span class="line">然后，我们讲解数组元素的访问和常见操作，这样我们就能够应用数组解决常见的问题了。</span><br><span class="line"></span><br><span class="line">最后，我们讲解数组内存图，这样我们就能够对数组在内存中的存储和操作做到知其然也知其所以然。</span><br><span class="line"></span><br><span class="line">好了，到此关于数组的概述和课程安排我们就先讲到这里。</span><br><span class="line"></span><br><span class="line">### 1.2 数组定义和静态初始化</span><br><span class="line"></span><br><span class="line">来，继续啊，下面我们来学习数组定义格式。</span><br><span class="line"></span><br><span class="line">数组有两种定义格式：</span><br><span class="line"></span><br><span class="line">- **格式1：**数据类型[] 变量名;</span><br><span class="line">- 范例：              `int[] arr;`</span><br><span class="line">- 定义了一个int类型的数组，数组名是arr</span><br><span class="line"></span><br><span class="line">- **格式2：**数据类型 变量名[];</span><br><span class="line">- 范例：              `int arr[];`</span><br><span class="line">- 定义了一个int类型的变量，变量名是arr数组</span><br><span class="line"></span><br><span class="line">这两种格式在使用上是没有区别的，但是在阅读的时候是有一个小区别的？</span><br><span class="line"></span><br><span class="line">我们先来看格式1：定义了一个int类型的数组，数组名是arr；再来看格式2：定义了一个int类型的变量，变量名是arr数组</span><br><span class="line"></span><br><span class="line">很明显，格式1明确的告诉了我们定义的是一个数组，所以，在定义数组的时候，我们**推荐使用格式1**。</span><br><span class="line"></span><br><span class="line">现在我们会定义数组了，但是数组中没有数据。而Java中的数组必须先初始化，然后才能使用。</span><br><span class="line"></span><br><span class="line">所以，接着，我们来说一下数组的初始化。</span><br><span class="line"></span><br><span class="line">- **所谓初始化：**就是为数组中的数组元素分配内存空间，并为每个数组元素赋值</span><br><span class="line"></span><br><span class="line">- 注意：数组中的每一个数据，我们称之为数组中的**元素**</span><br><span class="line"></span><br><span class="line">知道了什么是初始化后，我们来说一下数组中的初始化。</span><br><span class="line"></span><br><span class="line">数组的初始化分为两种方式：一种是静态初始化，一种是动态初始化。</span><br><span class="line"></span><br><span class="line">它们有各自的使用场景，后面我们会讲解到。</span><br><span class="line"></span><br><span class="line">这里我们先讲解数组的静态初始化</span><br><span class="line"></span><br><span class="line">- **静态初始化：**初始化时指定每个数组元素的初始值，由系统决定数组长度</span><br><span class="line">- **格式：**数据类型[] 变量名 = new 数据类型[]&#123;数据1,数据2,数据3,…&#125;;</span><br><span class="line">- **范例：**`int[] arr = new int[]&#123;1,2,3&#125;;`</span><br><span class="line"></span><br><span class="line">针对这种格式呢？Java提供了一种简化的格式，让我们使用起来更方便</span><br><span class="line"></span><br><span class="line">- **简化格式：**数据类型[] 变量名 = &#123;数据1,数据2,数据3,…&#125;;</span><br><span class="line">- **范例：**`int[] arr = &#123;1,2,3&#125;;`</span><br><span class="line"></span><br><span class="line">这样呢，我们就能够定义并初始化一个数组了。</span><br><span class="line"></span><br><span class="line">好了，关于数组的定义和静态初始化我们就先讲到这里。</span><br><span class="line"></span><br><span class="line">### 1.3 数组元素访问(获取和修改)</span><br><span class="line"></span><br><span class="line">来，继续啊，下面我们来学习数组元素访问。</span><br><span class="line"></span><br><span class="line">而在数组元素访问这一块，我们要学习两个知识：</span><br><span class="line"></span><br><span class="line">1. 一个是数组变量的访问方式</span><br><span class="line">2. 一个是数组内部保存的数据的访问方式</span><br><span class="line"></span><br><span class="line">我们先来说，数组变量访问方式</span><br><span class="line"></span><br><span class="line">- 它的格式是这样的：**数组名**</span><br><span class="line"></span><br><span class="line">我们可以通过输出语句，输出数组名，就能够得到一个数据。那这个数据是什么呢？不着急，一会我们会演示的。</span><br><span class="line"></span><br><span class="line">接着，我们来说，数组内部保存的数据的访问方式</span><br><span class="line"></span><br><span class="line">- 它的格式是：**数组名[索引]**</span><br><span class="line"></span><br><span class="line">这个格式没有什么问题，但是这里的索引是什么呢？为了讲解这个索引，我们来看一下这样的一个场景：</span><br><span class="line"></span><br><span class="line">在这里有一排人，在这排着队呢，为了知道他们站的位置，给他们进行1,2,3.。。。的编号。</span><br><span class="line"></span><br><span class="line">![1640179151155](imgs/1640179151155.png)</span><br><span class="line"></span><br><span class="line">这里的编号是从1开始的。</span><br><span class="line"></span><br><span class="line">而计算机中编号习惯从0开始，这个**从0开始的编号，我们就称之为索引**。</span><br><span class="line"></span><br><span class="line">![1640179188190](imgs/1640179188190.png)</span><br><span class="line"></span><br><span class="line">接着，我们来说一下：</span><br><span class="line"></span><br><span class="line">- 索引是数组中数据的编号方式</span><br><span class="line">- 作用：用于访问数组中的数据使用，数组名[索引]等同于变量名，是一种特殊的变量名</span><br><span class="line">- 它还有这样的几个特征：</span><br><span class="line">  - 特征①：索引从0开始</span><br><span class="line">  - 特征②：索引是连续的</span><br><span class="line">  - 特征③：索引逐一增加，每次加1</span><br><span class="line"></span><br><span class="line">知道了如何访问数组变量以及数组中的元素后，下面我们到idea中去演示一下。</span><br><span class="line"></span><br><span class="line">```java</span><br><span class="line">/*</span><br><span class="line">    数组变量访问方式</span><br><span class="line">        格式：数组名</span><br><span class="line"></span><br><span class="line">    数组内部保存的数据的访问方式</span><br><span class="line">        格式：数组名[索引]</span><br><span class="line"> */</span><br><span class="line">public class ArrayDemo &#123;</span><br><span class="line">    public static void main(String[] args) &#123;</span><br><span class="line">        //定义一个数组，并进行初始化</span><br><span class="line">//        int[] scores = new int[]&#123;93,96,99&#125;;</span><br><span class="line">        int[] scores = &#123;93,96,99&#125;;</span><br><span class="line"></span><br><span class="line">        //输出数组名</span><br><span class="line">        System.out.println(scores); //[I@776ec8df</span><br><span class="line"></span><br><span class="line">        //输出数组中的元素</span><br><span class="line">        System.out.println(scores[0]);</span><br><span class="line">        System.out.println(scores[1]);</span><br><span class="line">        System.out.println(scores[2]);</span><br><span class="line">        System.out.println(&quot;-----------&quot;);</span><br><span class="line"></span><br><span class="line">        //修改数组中的元素</span><br><span class="line">        scores[0] = 100;</span><br><span class="line">        scores[1] = 98;</span><br><span class="line">        scores[2] = 95;</span><br><span class="line">        //再次输出数组中的元素</span><br><span class="line">        System.out.println(scores[0]);</span><br><span class="line">        System.out.println(scores[1]);</span><br><span class="line">        System.out.println(scores[2]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="案例1数组常见操作之遍历">1.4 案例1(数组常见操作之遍历)</h3>
<p>来，继续啊，下面我们来学习数组常见操作，数组常见操作有很多，这里我们先讲解下面这几个：</p>
<ul>
<li>遍历</li>
<li>获取最大值</li>
<li>元素打乱</li>
</ul>
<p>其他的操作，后续我们会进行讲解。</p>
<p>首先，我们来讲解数组遍历，那什么是数组遍历？我们一起来说一下：</p>
<ul>
<li>数组遍历指的是：<strong>获取数组中的每一个元素</strong>，我们可以把获取到的元素输出在控制台</li>
</ul>
<p>知道了什么是数组遍历后，我们到IDEA中去实现一下，然后再回来总结。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    遍历</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//定义数组并初始化</span></span><br><span class="line">        <span class="type">int</span>[] arr = &#123;<span class="number">77</span>, <span class="number">88</span>, <span class="number">99</span>, <span class="number">66</span>, <span class="number">55</span>, <span class="number">44</span>&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取数组中的元素</span></span><br><span class="line"><span class="comment">//        System.out.println(arr[0]);</span></span><br><span class="line"><span class="comment">//        System.out.println(arr[1]);</span></span><br><span class="line"><span class="comment">//        System.out.println(arr[2]);</span></span><br><span class="line"><span class="comment">//        System.out.println(arr[3]);</span></span><br><span class="line"><span class="comment">//        System.out.println(arr[4]);</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//用循环改进</span></span><br><span class="line"><span class="comment">//        for (int i=0; i&lt;5; i++) &#123;</span></span><br><span class="line"><span class="comment">//            System.out.println(arr[i]);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//格式：数组名.length</span></span><br><span class="line"><span class="comment">//        System.out.println(arr.length);</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">0</span>; i&lt;arr.length; i++) &#123;</span><br><span class="line">            System.out.println(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>最后，我们回到资料总结一下：</p>
<p><strong>什么是数组遍历：</strong></p>
<ul>
<li>获取数组中的每一个元素，我们可以把获取到的元素输出在控制台</li>
</ul>
<p><strong>获取数组长度(元素个数)：</strong></p>
<ul>
<li>格式：数组名.length</li>
<li>范例：<font color="red"><strong>arr.length</strong></font></li>
</ul>
<p><strong>数组遍历<font color="red">通用格式</font>：</strong></p>
<figure>
<img data-src="imgs/1640179442957.png" alt="1640179442957">
<figcaption aria-hidden="true">1640179442957</figcaption>
</figure>
<p><strong>注意：</strong>数组遍历指的是把数组中的元素取出来，取出来之后可以(打印，求和，判断…)</p>
<p>数组遍历通用格式：</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">for (int i = 0; i &lt; scores.length; i++) &#123;</span><br><span class="line">       scores[i]</span><br><span class="line">   &#125;</span><br></pre></td></tr></table></figure>
<p>注意：数组遍历指的是把数组中的元素取出来，取出来之后可以(打印，求和，判断…)。
选择哪种操作，得根据我们的实际需求来看。</p>
<p>好了，关于数组常见操作之遍历我们就先讲到这里</p>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="案例2数组常见操作之获取最大值">1.5
案例2(数组常见操作之获取最大值)</h3>
<p>来，继续啊，下面我们来学习数组中获取最大值：</p>
<p>而在讲解具体的实现之前，我们先通过一个场景，让大家对最值有一个认知：</p>
<p>看这里，这是一组家庭成员：</p>
<figure>
<img data-src="imgs/1640179577860.png" alt="1640179577860">
<figcaption aria-hidden="true">1640179577860</figcaption>
</figure>
<p>给出一组数据，这些数据表示的是他们的年龄。68是最大年龄，4是最小年龄，所以，68和4就可以被称之为最值。</p>
<figure>
<img data-src="imgs/1640179610351.png" alt="1640179610351">
<figcaption aria-hidden="true">1640179610351</figcaption>
</figure>
<p>再给出一组数据，这些数据表示的是他们的身高，171是最高身高，81是最低身高，所以，171和81就可以被称之为最值。</p>
<figure>
<img data-src="imgs/1640179641419.png" alt="1640179641419">
<figcaption aria-hidden="true">1640179641419</figcaption>
</figure>
<p>那这里的171为什么就是最高身高呢？</p>
<p>很明显，要想说171是最高身高，我们就要把这些数据从头到尾比较了一遍，最终找到了最高身高171。</p>
<p>而要进行比较，我们的先把这些数据保存起来。这里我们就采用数组把这些数据给保存起来。</p>
<p>知道了大量同类型数据用数组保存和最值的知识后，我们来看一下我们的需求：</p>
<ul>
<li>我这里有一个数组，我现在要获取数组中的最大值</li>
</ul>
<figure>
<img data-src="imgs/1640179738721.png" alt="1640179738721">
<figcaption aria-hidden="true">1640179738721</figcaption>
</figure>
<p>如何实现呢？接下来，我们说一下思路：</p>
<p>1：因为我们最终，要得到最大值，所以，这里我们定义一个变量，用于保存最大值。</p>
<p>但是，它没有初始值，给多少比较合适呢？我总不能拿数组以外的数据进来吧，因为我们最终获取的是数组中的最大值。所以，我们就要从数组中找一个值作为初始值。</p>
<p>一般来说，我们取数组中第一个元素作为参照值。</p>
<p>2：这样我们的最大值变量就有初始值了，接下来，我们与数组中剩余的数据逐个比较，比完之后，max中最终保存的是最大值。</p>
<p>这个动作怎么实现呢？我们来说一下：获取索引1-4，为什么不从0开始呢？因为0索引位置的值已经作为初始值了。</p>
<p>3：我们每次比较，需要把最大值保存到max变量中，这个动作，我们可以通过if语句实现。</p>
<p>4：当循环结束后，打印max变量就可以了，这个max里面保存的就是最大值。</p>
<p>说完了数组获取最大值的思路后，我们到idea中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    获取最大值</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//定义数组</span></span><br><span class="line">        <span class="type">int</span>[] arr = &#123;<span class="number">12</span>, <span class="number">45</span>, <span class="number">98</span>, <span class="number">73</span>, <span class="number">60</span>&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//定义变量max存储最大值，取第一个数据为变量的初始值</span></span><br><span class="line">        <span class="type">int</span> <span class="variable">max</span> <span class="operator">=</span> arr[<span class="number">0</span>];</span><br><span class="line"></span><br><span class="line">        <span class="comment">//与数组中剩余数据逐个比对，每次比对将最大值保存到变量max中</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> i=<span class="number">1</span>; i&lt;arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span>(arr[i] &gt; max) &#123;</span><br><span class="line">                max = arr[i];</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//循环结束后输出变量max的值</span></span><br><span class="line">        System.out.println(<span class="string">&quot;max:&quot;</span> + max);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="案例3数组常见操作之元素打乱">1.6
案例3(数组常见操作之元素打乱)</h3>
<p>来，继续啊，下面我们来学习数组中元素打乱。</p>
<p>那什么是元素打乱呢？</p>
<ul>
<li>元素打乱就是把数组中的元素随机交换位置，每次运行都可能产生不一样的结果</li>
</ul>
<p>比如：<strong>arr</strong> <strong>= {12, 45, 98, 73,
60};</strong></p>
<p><strong>遍历：</strong></p>
<ul>
<li>正常情况下：12,45,98,73,60</li>
<li>元素打乱后：45,73,12,60,98</li>
</ul>
<p><strong>涉及到的操作：</strong></p>
<ul>
<li>获取数组中元素的随机索引
<ul>
<li><code>Random r = new Random();</code></li>
<li><code>int index = r.nextInt(arr.length);</code></li>
</ul></li>
<li>数组中元素交换</li>
</ul>
<p>获取随机索引比较好实现，但是元素交换该如何实现呢？</p>
<p>这里为了帮助大家更好的理解，我们给出这样的一个场景来理解数据交换：</p>
<p>把啤酒杯和红酒杯里面的酒进行交换</p>
<figure>
<img data-src="imgs/1640180154556.png" alt="1640180154556">
<figcaption aria-hidden="true">1640180154556</figcaption>
</figure>
<p>如何实现呢？</p>
<p>第一步：找来一个同类型的空酒杯</p>
<figure>
<img data-src="imgs/1640180271885.png" alt="1640180271885">
<figcaption aria-hidden="true">1640180271885</figcaption>
</figure>
<p>第二步：把红酒倒入到空酒杯中</p>
<figure>
<img data-src="imgs/1640180296416.png" alt="1640180296416">
<figcaption aria-hidden="true">1640180296416</figcaption>
</figure>
<p>第三步：把啤酒倒入到曾经装红酒的杯子中</p>
<figure>
<img data-src="imgs/1640180317142.png" alt="1640180317142">
<figcaption aria-hidden="true">1640180317142</figcaption>
</figure>
<p>第四步：把红酒从临时找来的杯子中倒入到曾经装啤酒的杯子中</p>
<figure>
<img data-src="imgs/1640180362775.png" alt="1640180362775">
<figcaption aria-hidden="true">1640180362775</figcaption>
</figure>
<p>这样我们就实现了啤酒杯和红酒杯里面的酒进行交换。这种思想同样也适合我们程序中的数据交换。</p>
<p>下面我们到IDEA中去演示一下数据的交换：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    数据交换</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">DataSwap</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line"><span class="comment">//        int a = 10;</span></span><br><span class="line"><span class="comment">//        int b = 20;</span></span><br><span class="line"><span class="comment">//        System.out.println(&quot;交换前：a=&quot; + a + &quot;,b=&quot; + b);</span></span><br><span class="line"><span class="comment">//        int temp = a;</span></span><br><span class="line"><span class="comment">//        a = b;</span></span><br><span class="line"><span class="comment">//        b = temp;</span></span><br><span class="line"><span class="comment">//        System.out.println(&quot;交换后：a=&quot; + a + &quot;,b=&quot; + b);</span></span><br><span class="line"></span><br><span class="line">        <span class="type">int</span>[] arr = &#123;<span class="number">11</span>, <span class="number">22</span>, <span class="number">33</span>, <span class="number">44</span>, <span class="number">55</span>&#125;;</span><br><span class="line">        System.out.println(<span class="string">&quot;交换前：arr[0]=&quot;</span> + arr[<span class="number">0</span>] + <span class="string">&quot;,arr[4]=&quot;</span> + arr[<span class="number">4</span>]);</span><br><span class="line">        <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> arr[<span class="number">0</span>];</span><br><span class="line">        arr[<span class="number">0</span>] = arr[<span class="number">4</span>];</span><br><span class="line">        arr[<span class="number">4</span>] = temp;</span><br><span class="line">        System.out.println(<span class="string">&quot;交换后：arr[0]=&quot;</span> + arr[<span class="number">0</span>] + <span class="string">&quot;,arr[4]=&quot;</span> + arr[<span class="number">4</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>数据的交换演示完毕后，下面我们就可以来实现把数组中的元素打乱了：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    元素打乱</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest03</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//定义数组</span></span><br><span class="line">        <span class="type">int</span>[] arr = &#123;<span class="number">12</span>, <span class="number">45</span>, <span class="number">98</span>, <span class="number">73</span>, <span class="number">60</span>&#125;;</span><br><span class="line"></span><br><span class="line"><span class="comment">//        Random r = new Random();</span></span><br><span class="line"><span class="comment">//        int index = r.nextInt(arr.length);</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        //第一次交换</span></span><br><span class="line"><span class="comment">//        int temp = arr[0];</span></span><br><span class="line"><span class="comment">//        arr[0] = arr[index];</span></span><br><span class="line"><span class="comment">//        arr[index] = temp;</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        //第二次交换</span></span><br><span class="line"><span class="comment">//        index = r.nextInt(arr.length);</span></span><br><span class="line"><span class="comment">//        temp = arr[1];</span></span><br><span class="line"><span class="comment">//        arr[1] = arr[index];</span></span><br><span class="line"><span class="comment">//        arr[index] = temp;</span></span><br><span class="line"></span><br><span class="line">        <span class="type">Random</span> <span class="variable">r</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="type">int</span> <span class="variable">index</span> <span class="operator">=</span> r.nextInt(arr.length);</span><br><span class="line"></span><br><span class="line">            <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> arr[i];</span><br><span class="line">            arr[i] = arr[index];</span><br><span class="line">            arr[index] = temp;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历数组</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            System.out.println(arr[i]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="数组动态初始化">1.7 数组动态初始化</h3>
<p>来，继续啊，下面我们来学习数组初始化之动态初始化。</p>
<p>前面呢，我们已经学习过了静态初始化，也就是初始化时指定每个数组元素的初始值。</p>
<p>而动态初始化呢？初始化时只指定数组长度，由系统为数组分配初始值</p>
<p>这里我们来看一下动态初始化的格式：</p>
<ul>
<li><strong>格式：</strong>数据类型[] 变量名 = new
数据类型[数组长度];</li>
<li><strong>范例：</strong>int[] arr = new int[3];</li>
</ul>
<p>表示我们定义了一个int类型的数组，初始化为3个元素。</p>
<p>知道了如何对数组进行动态初始化之后，下面我们到IDEA中去使用一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    动态初始化：初始化时只指定数组长度，由系统为数组分配初始值</span></span><br><span class="line"><span class="comment">    格式：数据类型[] 变量名 = new 数据类型[数组长度];</span></span><br><span class="line"><span class="comment">    范例：int[] arr = new int[3];</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//格式：数据类型[] 变量名 = new 数据类型[数组长度];</span></span><br><span class="line">        <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">        <span class="comment">//输出数组名</span></span><br><span class="line">        System.out.println(arr); <span class="comment">//[I@776ec8df</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//输出数组中的元素</span></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>]);</span><br><span class="line">        System.out.println(<span class="string">&quot;------------&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//修改数组中的元素</span></span><br><span class="line">        arr[<span class="number">0</span>] = <span class="number">10</span>;</span><br><span class="line">        arr[<span class="number">1</span>] = <span class="number">20</span>;</span><br><span class="line">        arr[<span class="number">2</span>] = <span class="number">30</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//再次输出数组中的元素</span></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>演示完毕之后，回到资料我们总结一下数组初始化的两种方式的<strong>各自使用场景：</strong></p>
<ul>
<li><strong>静态初始化：</strong>开始就存入元素值，适合一开始就能确定元素值的业务场景</li>
<li><strong>动态初始化：</strong>指定数组长度，后期赋值，适合开始知道数据的数量，但是不确定具体元素值的业务场景</li>
<li><strong>注意：</strong>两种初始化的方式是独立的，不可以混用</li>
</ul>
<figure>
<img data-src="imgs/1640180957760.png" alt="1640180957760">
<figcaption aria-hidden="true">1640180957760</figcaption>
</figure>
<p>好了，关于数组初始化之动态初始化我们就先讲到这里。</p>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="案例4数组元来自键盘录入">1.8 案例4(数组元来自键盘录入)</h3>
<p>来，继续啊，下面我们来做一个案例：数组元素来自键盘录入</p>
<p>首先呢，我们来看一下具体的需求：</p>
<ul>
<li><strong>需求：</strong>定义一个可以存储5个元素的int数组，数据来自于键盘录入，最后遍历数组，把元素输出在控制台</li>
</ul>
<p>首先，我们来简单的分析一下：</p>
<p>这个数组可以存储5个int类型的元素，但是数据来自于键盘录入，也就是先指定数组长度，后期赋值。</p>
<p>这很明显适合使用数组的动态初始化，所以，这里我们将采用动态初始化对数组进行初始化。</p>
<p>接着，数据来自于键盘录入，我们得知道这里要使用Scanner来实现。</p>
<p>明确了这两个内容后，下面我们到IDEA中一起去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Scanner;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    需求：定义一个可以存储5个元素的int数组，数据来自于键盘录入，最后遍历数组，把元素输出在控制台</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//定义一个数组，并进行动态初始化</span></span><br><span class="line">        <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">5</span>];</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建键盘录入对象</span></span><br><span class="line">        <span class="type">Scanner</span> <span class="variable">sc</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Scanner</span>(System.in);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        //录入第1个数据</span></span><br><span class="line"><span class="comment">//        System.out.println(&quot;请输入第1个数据：&quot;);</span></span><br><span class="line"><span class="comment">////        int i = sc.nextInt();</span></span><br><span class="line"><span class="comment">////        arr[0] = i;</span></span><br><span class="line"><span class="comment">//        arr[0] = sc.nextInt();</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        //录入第2个数据</span></span><br><span class="line"><span class="comment">//        System.out.println(&quot;请输入第2个数据：&quot;);</span></span><br><span class="line"><span class="comment">//        arr[1] = sc.nextInt();</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            System.out.println(<span class="string">&quot;请输入第&quot;</span> + (i + <span class="number">1</span>) + <span class="string">&quot;个元素：&quot;</span>);</span><br><span class="line">            arr[i] = sc.nextInt();</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        printArray(arr);</span><br><span class="line"></span><br><span class="line">            <span class="comment">//输出内容并换行</span></span><br><span class="line"><span class="comment">//        System.out.println(&quot;hello&quot;);</span></span><br><span class="line"><span class="comment">//        System.out.println(&quot;world&quot;);</span></span><br><span class="line">            <span class="comment">//输出内容不换行</span></span><br><span class="line"><span class="comment">//        System.out.print(&quot;hello&quot;);</span></span><br><span class="line"><span class="comment">//        System.out.print(&quot;world&quot;);</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">printArray</span><span class="params">(<span class="type">int</span>[] arr)</span> &#123;</span><br><span class="line">        System.out.print(<span class="string">&quot;[&quot;</span>);</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">if</span>(i==arr.length-<span class="number">1</span>) &#123;</span><br><span class="line">                System.out.print(arr[i]);</span><br><span class="line">            &#125;<span class="keyword">else</span> &#123;</span><br><span class="line">                System.out.print(arr[i] + <span class="string">&quot;, &quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line">        System.out.println(<span class="string">&quot;]&quot;</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">/*</span></span><br><span class="line"><span class="comment">        参数：int[] arr</span></span><br><span class="line"><span class="comment">        返回值：void</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line"><span class="comment">//    public static void printArray(int[] arr) &#123;</span></span><br><span class="line"><span class="comment">//        for (int i = 0; i &lt; arr.length; i++) &#123;</span></span><br><span class="line"><span class="comment">//            System.out.println(arr[i]);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//    &#125;</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在讲解过程中，我们讲到了一个新的输出语句，不带ln的方法，它只输出内容，不换行。</p>
<p>而以前我们使用的输出语句，不仅仅把内容输出，而且在输出内容后，会加一个换行。</p>
<p>好了，关于案例数组元素来自键盘录入我们就先讲到这里。</p>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="数组内存图">1.9 数组内存图</h3>
<p>来，继续啊，下面我们来学习数组内存图。</p>
<p>这里呢，我们通过单个数组的内存图让大家了解一下数组在内存中的存储和使用就可以了。</p>
<p>整个过程讲解的是下图代码的执行流程，大家能够听懂即可。</p>
<figure>
<img data-src="imgs/1640181579146.png" alt="1640181579146">
<figcaption aria-hidden="true">1640181579146</figcaption>
</figure>
<p>最后，我们自己编写代码验证一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    数组内存图代码</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">3</span>];</span><br><span class="line"></span><br><span class="line">        <span class="comment">//输出数组名及元素</span></span><br><span class="line">        System.out.println(arr);</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//给数组中的元素赋值</span></span><br><span class="line">        arr[<span class="number">0</span>] = <span class="number">100</span>;</span><br><span class="line">        arr[<span class="number">2</span>] = <span class="number">200</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//再次输出数组名及元素</span></span><br><span class="line">        System.out.println(arr);</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">2</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于仅仅是验证讲解的结论，故不需要练习。</p>
<h3 id="数组使用中的两个小问题">1.10 数组使用中的两个小问题</h3>
<p>来，继续啊，下面我们来讲解一下数组使用中的两个小问题。</p>
<p>先来看第一个问题：请问下列代码有问题吗？如果有，是什么问题？如何解决？</p>
<figure>
<img data-src="imgs/1640181720120.png" alt="1640181720120">
<figcaption aria-hidden="true">1640181720120</figcaption>
</figure>
<p>通过内存图进行讲解：</p>
<figure>
<img data-src="imgs/1640181750266.png" alt="1640181750266">
<figcaption aria-hidden="true">1640181750266</figcaption>
</figure>
<p>最终结论是：</p>
<ul>
<li>有问题</li>
<li>访问了不存在的索引位置元素</li>
<li>修改不存在的索引为正确的索引。范围(0~数组长度-1)</li>
</ul>
<p>再来看第二个问题：请问下列代码有问题吗？如果有，是什么问题？如何解决？</p>
<figure>
<img data-src="imgs/1640181788943.png" alt="1640181788943">
<figcaption aria-hidden="true">1640181788943</figcaption>
</figure>
<p>通过内存图进行讲解：</p>
<figure>
<img data-src="imgs/1640181816425.png" alt="1640181816425">
<figcaption aria-hidden="true">1640181816425</figcaption>
</figure>
<p>最终结论是：</p>
<ul>
<li>有问题</li>
<li>对象设置为null，不在指向堆内存数据了，还想继续访问堆内存数据</li>
<li>对象在使用前，进行不为null的判断</li>
</ul>
<p>讲解完毕后，到IDEA中再去通过代码验证一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    数组使用中的两个小问题</span></span><br><span class="line"><span class="comment">        1：索引越界：访问了数组中不存在的索引对应的元素，造成索引越界问题</span></span><br><span class="line"><span class="comment">            ArrayIndexOutOfBoundsException</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        2：空指针异常：对象不再指向堆内存，还想继续访问数据，访问失败</span></span><br><span class="line"><span class="comment">            NullPointerException</span></span><br><span class="line"><span class="comment"></span></span><br><span class="line"><span class="comment">        null：空值，引用数据类型的默认值，表示不指向任何有效对象</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="type">int</span>[] arr = <span class="keyword">new</span> <span class="title class_">int</span>[<span class="number">3</span>];</span><br><span class="line">        <span class="comment">//输出元素</span></span><br><span class="line">        <span class="comment">//Exception in thread &quot;main&quot; java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3</span></span><br><span class="line"><span class="comment">//        System.out.println(arr[3]);</span></span><br><span class="line">        System.out.println(arr[<span class="number">2</span>]);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//把null赋值给arr</span></span><br><span class="line">        arr = <span class="literal">null</span>;</span><br><span class="line">        <span class="comment">//输出元素</span></span><br><span class="line">        <span class="comment">//Exception in thread &quot;main&quot; java.lang.NullPointerException: Cannot load from int array because &quot;arr&quot; is null</span></span><br><span class="line">        <span class="keyword">if</span>(arr != <span class="literal">null</span>) &#123;</span><br><span class="line">            System.out.println(arr[<span class="number">0</span>]);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于仅仅是验证讲解的结论，故不需要练习。</p>
<h2 id="二维数组">2：二维数组</h2>
<h3 id="二维数组概述和课程安排">2.1 二维数组概述和课程安排</h3>
<p>来，继续啊，下面我们来学习二维数组。前面我们讲解的数组，一般称之为一维数组。</p>
<p>为了帮助大家理解二维数组呢？这里我们来看这样的一个场景：</p>
<p>需求：我们要存储多个班级的学生的考试成绩，该怎么办呢？</p>
<p>如果要存储一个班级中多个学生的考试成绩，我们就可以采用数组来存储。</p>
<p>现在的问题是？多个班级的考试成绩，我们针对每个班级都采用数组存储。</p>
<p>第一个班级：数组1</p>
<p>第二个班级：数组2</p>
<p>第三个班级：数组3 …</p>
<p>但是多个班级，也可以采用数组存储啊。所以，Java就提供了二维数组供我们使用。</p>
<p>这里我们给二维数组一个简单的定义：</p>
<ul>
<li><strong>二维数组：</strong>元素为一维数组的数组</li>
</ul>
<p>知道了什么是二维数组后，我们再说一下二维数组的定义格式：</p>
<p>它有这样的三个格式，用[][]表示二维数组：</p>
<blockquote>
<ol type="1">
<li>数据类型[] [] 变量名; int[] [] arr; <strong>推荐</strong></li>
<li>数据类型 变量名[] []; int arr[] [];</li>
<li>数据类型[] 变量名[]; int[] arr[];</li>
</ol>
</blockquote>
<p>推荐使用第一种格式。</p>
<p>知道了什么是二维数组以及二维数组的定义格式后，我们来说一下二维数组这一块的课程安排：</p>
<figure>
<img data-src="imgs/1640182140083.png" alt="1640182140083">
<figcaption aria-hidden="true">1640182140083</figcaption>
</figure>
<p>首先，我们讲解二维数组初始化，这样我们就能够给二维数组中的元素赋值了。</p>
<p>接着，我们讲解二维数组元素访问，这样我们就能够获取到二维数组中的数据了。</p>
<p>然后，我们讲解二维数组内存图，这样我们就能够对二维数组在内存中的存储和操作做到知其然也知其所以然。</p>
<p>最后，我们讲解二维数组常见操作，这样我们就能够应用二维数组解决常见的问题了。</p>
<p>好了，到此关于二维数组的概述和课程安排我们就先讲到这里。</p>
<h3 id="二维数组初始化">2.2 二维数组初始化</h3>
<p>来，继续啊，下面我们来学习二维数组初始化，和一维数组一样，二维数组初始化也有两种格式：</p>
<ol type="1">
<li>静态初始化</li>
<li>动态初始化</li>
</ol>
<p>首先来看<strong>静态初始化：</strong></p>
<ul>
<li><strong>格式：</strong><code>数据类型[] [] 变量名 = new 数据类型[] []&#123;&#123;元素…&#125;,&#123;元素…&#125;,&#123;元素…&#125;,…&#125;;`
- **范例：**`int[] [] arr = new int[] []&#123;&#123;1,2,3&#125;,&#123;4,5,6&#125;,&#123;7,8,9&#125;&#125;;</code></li>
</ul>
<p><strong>解读：</strong></p>
<ul>
<li>定义了一个二维数组</li>
<li>二维数组中有三个元素(一维数组)</li>
<li>每一个一维数组有三个元素(int类型数据)</li>
<li>注意：一维数组中元素的个位可以是不同的</li>
<li>​ 比如：
<code>int[] [] arr = new int[] []&#123;&#123;1,2,3&#125;,&#123;4,5&#125;,&#123;6,7,8,9&#125;&#125;;</code></li>
</ul>
<p>同样，针对静态初始化，二维数组也给出了简化格式：</p>
<ul>
<li><strong>简化格式：</strong><code>数据类型[][] 变量名 = &#123;&#123;元素…&#125;,&#123;元素…&#125;,&#123;元素…&#125;,…&#125;;`
- **范例：**`int[] [] arr = &#123;&#123;1,2,3&#125;,&#123;4,5,6&#125;,&#123;7,8,9&#125;&#125;;</code></li>
</ul>
<p>再来看<strong>动态初始化：</strong></p>
<ul>
<li><strong>格式：</strong><code>数据类型[] [] 变量名 = new 数据类型[m] [n];</code></li>
<li><strong>范例：</strong><code>int[] [] arr = new int[2] [3];</code></li>
</ul>
<p><strong>解读：</strong></p>
<ul>
<li>定义了一个二维数组</li>
<li>二维数组中有2个元素(一维数组)</li>
<li>每一个一维数组有3个元素(int类型数据)</li>
</ul>
<p>他们的各自使用场景和一维数组中讲解的是一样的：</p>
<p>静态初始化：适合一开始就能确定元素值的业务场景</p>
<p>动态初始化：适合开始知道数据的数量，但是不确定具体元素值的业务场景</p>
<p>好了，关于二维数组的初始化我们就先讲到这里</p>
<h3 id="二维数组元素访问">2.3 二维数组元素访问</h3>
<p>来，继续啊，下面我们来学习二维数组元素访问。</p>
<p>关于二维数组的元素访问，我们先到idea中去讲解，然后再回来总结：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    静态初始化简化格式：数据类型[][] 数组名 = &#123;&#123;元素...&#125;,&#123;元素...&#125;,&#123;元素...&#125;,...&#125;;</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayDemo</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//静态初始化简化格式：数据类型[][] 数组名 = &#123;&#123;元素...&#125;,&#123;元素...&#125;,&#123;元素...&#125;,...&#125;;</span></span><br><span class="line">        <span class="type">int</span>[][] arr = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//输出数组名</span></span><br><span class="line">        System.out.println(arr); <span class="comment">//[[I@776ec8df</span></span><br><span class="line"></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>]); <span class="comment">//[I@4eec7777</span></span><br><span class="line">        System.out.println(arr[<span class="number">1</span>]); <span class="comment">//[I@3b07d329</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//如何获取到数据1,2,3呢？</span></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">0</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">2</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，回到资料总结一下：</p>
<ul>
<li>获取二维数组：<strong>数组名</strong></li>
<li>获取每一个一维数组：<strong>数组名[索引]</strong></li>
<li>获取每一个二维数组元素：<strong>数组名[索引] [索引]</strong></li>
</ul>
<p>好了，关于二维数组的元素访问我们就先讲到这里。</p>
<h3 id="二维数组内存图">2.4 二维数组内存图</h3>
<p>来，继续啊，下面我们来学习二维数组的内存图。</p>
<p>这里呢，我们通过内存图的方式让大家了解一下二维数组在内存中的存储和使用就可以了。</p>
<p>整个过程讲解的是下图代码的执行流程，大家能够听懂即可。</p>
<figure>
<img data-src="imgs/1640182579200.png" alt="1640182579200">
<figcaption aria-hidden="true">1640182579200</figcaption>
</figure>
<p>最后，我们自己编写代码验证一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    二维数组内存图代码</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//定义二维数组</span></span><br><span class="line">        <span class="type">int</span>[][] arr = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//输出数组中的元素</span></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">1</span>]);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//修改数组中的元素</span></span><br><span class="line">        arr[<span class="number">0</span>][<span class="number">1</span>] = <span class="number">10</span>;</span><br><span class="line">        arr[<span class="number">1</span>][<span class="number">1</span>] = <span class="number">20</span>;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//再次输出数组中的元素</span></span><br><span class="line">        System.out.println(arr[<span class="number">0</span>][<span class="number">1</span>]);</span><br><span class="line">        System.out.println(arr[<span class="number">1</span>][<span class="number">1</span>]);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由于仅仅是验证讲解的结论，故不需要练习。</p>
<h3 id="案例5二维数组常见操作之遍历">2.5
案例5(二维数组常见操作之遍历)</h3>
<p>来，继续啊，下面我们来学习二维数组常见操作：</p>
<p>这里我们讲解两个操作：</p>
<ul>
<li>遍历</li>
<li>元素打乱</li>
</ul>
<p>首先，我们来看二维数组遍历。</p>
<p>这里有一个需求：已知一个二维数组
<code>arr = &#123;&#123;1,2,3&#125;,&#123;4,5,6&#125;,&#123;7,8,9&#125;&#125;;</code> 请把元素在控制台输出。</p>
<p>看完需求之后，我们到IDEA中一起去实现一下，然后再回来总结：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    需求：已知一个二维数组 arr = &#123;&#123;1,2,3&#125;,&#123;4,5,6&#125;,&#123;7,8,9&#125;&#125;; 请把元素在控制台输出</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest01</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//定义二维数组，并进行静态初始化</span></span><br><span class="line">        <span class="type">int</span>[][] arr = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;, &#123;<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>&#125;&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//获取数据1,2,3</span></span><br><span class="line"><span class="comment">//        System.out.println(arr[0][0]);</span></span><br><span class="line"><span class="comment">//        System.out.println(arr[0][1]);</span></span><br><span class="line"><span class="comment">//        System.out.println(arr[0][2]);</span></span><br><span class="line"><span class="comment">//        for (int i = 0; i &lt; arr[0].length; i++) &#123;</span></span><br><span class="line"><span class="comment">//            System.out.println(arr[0][i]);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        //获取数据4,5,6</span></span><br><span class="line"><span class="comment">////        System.out.println(arr[1][0]);</span></span><br><span class="line"><span class="comment">////        System.out.println(arr[1][1]);</span></span><br><span class="line"><span class="comment">////        System.out.println(arr[1][2]);</span></span><br><span class="line"><span class="comment">//        for (int i = 0; i &lt; arr[1].length; i++) &#123;</span></span><br><span class="line"><span class="comment">//            System.out.println(arr[1][i]);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        //获取数据7,8,9</span></span><br><span class="line"><span class="comment">////        System.out.println(arr[2][0]);</span></span><br><span class="line"><span class="comment">////        System.out.println(arr[2][1]);</span></span><br><span class="line"><span class="comment">////        System.out.println(arr[2][2]);</span></span><br><span class="line"><span class="comment">//        for (int i = 0; i &lt; arr[2].length; i++) &#123;</span></span><br><span class="line"><span class="comment">//            System.out.println(arr[2][i]);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="comment">//arr[i]</span></span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; arr[i].length; j++) &#123;</span><br><span class="line">                System.out.print(arr[i][j]+<span class="string">&quot; &quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在代码讲解过程中，我们讲解了循环嵌套：for循环的语句体是循环语句，这种现象被称为循环嵌套。</p>
<p>讲解完毕后，回到资料中我们去总结一下：</p>
<p>在实现的过程中，我们在二维数组中使用了循环嵌套：</p>
<p>① 循环嵌套：循环语句中嵌套循环语句</p>
<p>② 通过外层循环可以得到一维数组</p>
<p>③ 在通过内存循环可以得到每一个二维数组元素</p>
<p>好了，关于二维数组常见操作之遍历我们就先讲到这里。</p>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="案例6二维数组常见操作之元素打乱">2.6
案例6(二维数组常见操作之元素打乱)</h3>
<p>来，继续啊，下面我们来学习二维数组中元素打乱。</p>
<p>在前面呢，我们讲解过一维数组中的元素打乱，这里的思路和前面是一样的，也是要随机产生二维数组中元素的索引，并进行元素交换。</p>
<p>只不过呢，二维数组元素的索引是两个值，所以，这里要产生两个随机数。</p>
<p>知道了二维数组元素打乱的基本思路后，下面我们到IDEA中去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    需求：已知二维数组 arr = &#123;&#123;1,2,3&#125;,&#123;4,5,6&#125;,&#123;7,8,9&#125;&#125;;用程序实现把数组中的元素打乱,并在控制台输出打乱后的数组元素</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest02</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//定义二维数组，并进行静态初始化</span></span><br><span class="line">        <span class="type">int</span>[][] arr = &#123;&#123;<span class="number">1</span>, <span class="number">2</span>, <span class="number">3</span>&#125;, &#123;<span class="number">4</span>, <span class="number">5</span>, <span class="number">6</span>&#125;, &#123;<span class="number">7</span>, <span class="number">8</span>, <span class="number">9</span>&#125;&#125;;</span><br><span class="line"></span><br><span class="line">        <span class="comment">//创建随机数对象</span></span><br><span class="line">        <span class="type">Random</span> <span class="variable">r</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line"></span><br><span class="line">        <span class="comment">//遍历二维数组，进行元素打乱</span></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; arr[i].length; j++) &#123;</span><br><span class="line">                <span class="comment">//arr[i][j]</span></span><br><span class="line">                <span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> r.nextInt(arr.length);</span><br><span class="line">                <span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> r.nextInt(arr[x].length);</span><br><span class="line"></span><br><span class="line">                <span class="comment">//元素交换</span></span><br><span class="line">                <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> arr[i][j];</span><br><span class="line">                arr[i][j] = arr[x][y];</span><br><span class="line">                arr[x][y] = temp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; arr.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; arr[i].length; j++) &#123;</span><br><span class="line">                System.out.print(arr[i][j] + <span class="string">&quot; &quot;</span>);</span><br><span class="line">            &#125;</span><br><span class="line">            System.out.println();</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>好了，关于二维数组常见操作之元素打乱我们就先讲到这里</p>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="案例7图片展示">2.7 案例7(图片展示)</h3>
<p>来，继续啊，下面我们来做一个案例图片展示。</p>
<p>关于图片展示呢？前面我们是学习过的，通过JLable组件就可以实现图片展示了。</p>
<p>看一下，我们这里的需求，我有很多小的图片，它们拼在一起就成了这样的一张美女美图。</p>
<figure>
<img data-src="imgs/1640182850919.png" alt="1640182850919">
<figcaption aria-hidden="true">1640182850919</figcaption>
</figure>
<p>下面我们要做的事情就是把这些图片一张张的给展示出来。</p>
<p>这里的窗体我们已经准备好了，我们要做的事情就是在窗体上显示这些图片。</p>
<p>下面我们到IDEA中去实现一下：</p>
<p>图片素材在今天的资料里面，复制到当前的模块目录下。</p>
<figure>
<img data-src="imgs/1640183100572.png" alt="1640183100572">
<figcaption aria-hidden="true">1640183100572</figcaption>
</figure>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    图片展示</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest03</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;图片展示&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">380</span>, <span class="number">400</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">//        JLabel jLabel1 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\1.png&quot;));</span></span><br><span class="line"><span class="comment">//        jLabel1.setBounds(0, 0, 90, 90);</span></span><br><span class="line"><span class="comment">//        jf.add(jLabel1);</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        JLabel jLabel2 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\2.png&quot;));</span></span><br><span class="line"><span class="comment">//        jLabel2.setBounds(90, 0, 90, 90);</span></span><br><span class="line"><span class="comment">//        jf.add(jLabel2);</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        JLabel jLabel3 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\3.png&quot;));</span></span><br><span class="line"><span class="comment">//        jLabel3.setBounds(180, 0, 90, 90);</span></span><br><span class="line"><span class="comment">//        jf.add(jLabel3);</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">//        JLabel jLabel4 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\4.png&quot;));</span></span><br><span class="line"><span class="comment">//        jLabel4.setBounds(270, 0, 90, 90);</span></span><br><span class="line"><span class="comment">//        jf.add(jLabel4);</span></span><br><span class="line"></span><br><span class="line"><span class="comment">//        for (int i=0; i&lt;4; i++) &#123;</span></span><br><span class="line"><span class="comment">//            JLabel jLabel = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\&quot;+(i+1)+&quot;.png&quot;));</span></span><br><span class="line"><span class="comment">//            jLabel.setBounds(i*90,0,90,90);</span></span><br><span class="line"><span class="comment">//            jf.add(jLabel);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">////        JLabel jLabel5 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\5.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel5.setBounds(0, 90, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel5);</span></span><br><span class="line"><span class="comment">////</span></span><br><span class="line"><span class="comment">////        JLabel jLabel6 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\6.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel6.setBounds(90, 90, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel6);</span></span><br><span class="line"><span class="comment">////</span></span><br><span class="line"><span class="comment">////        JLabel jLabel7 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\7.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel7.setBounds(180, 90, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel7);</span></span><br><span class="line"><span class="comment">////</span></span><br><span class="line"><span class="comment">////        JLabel jLabel8 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\8.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel8.setBounds(270, 90, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel8);</span></span><br><span class="line"><span class="comment">//        for (int i=0; i&lt;4; i++) &#123;</span></span><br><span class="line"><span class="comment">//            JLabel jLabel = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\&quot;+(i+5)+&quot;.png&quot;));</span></span><br><span class="line"><span class="comment">//            jLabel.setBounds(i*90,90,90,90);</span></span><br><span class="line"><span class="comment">//            jf.add(jLabel);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">////        JLabel jLabel9 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\9.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel9.setBounds(0, 180, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel9);</span></span><br><span class="line"><span class="comment">////</span></span><br><span class="line"><span class="comment">////        JLabel jLabel10 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\10.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel10.setBounds(90, 180, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel10);</span></span><br><span class="line"><span class="comment">////</span></span><br><span class="line"><span class="comment">////        JLabel jLabel11 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\11.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel11.setBounds(180, 180, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel11);</span></span><br><span class="line"><span class="comment">////</span></span><br><span class="line"><span class="comment">////        JLabel jLabel12 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\12.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel12.setBounds(270, 180, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel12);</span></span><br><span class="line"><span class="comment">//        for (int i=0; i&lt;4; i++) &#123;</span></span><br><span class="line"><span class="comment">//            JLabel jLabel = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\&quot;+(i+9)+&quot;.png&quot;));</span></span><br><span class="line"><span class="comment">//            jLabel.setBounds(i*90,180,90,90);</span></span><br><span class="line"><span class="comment">//            jf.add(jLabel);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"><span class="comment">//</span></span><br><span class="line"><span class="comment">////        JLabel jLabel13 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\13.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel13.setBounds(0, 270, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel13);</span></span><br><span class="line"><span class="comment">////</span></span><br><span class="line"><span class="comment">////        JLabel jLabel14 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\14.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel14.setBounds(90, 270, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel14);</span></span><br><span class="line"><span class="comment">////</span></span><br><span class="line"><span class="comment">////        JLabel jLabel15 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\15.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel15.setBounds(180, 270, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel15);</span></span><br><span class="line"><span class="comment">////</span></span><br><span class="line"><span class="comment">////        JLabel jLabel16 = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\16.png&quot;));</span></span><br><span class="line"><span class="comment">////        jLabel16.setBounds(270, 270, 90, 90);</span></span><br><span class="line"><span class="comment">////        jf.add(jLabel16);</span></span><br><span class="line"><span class="comment">//        for (int i=0; i&lt;4; i++) &#123;</span></span><br><span class="line"><span class="comment">//            JLabel jLabel = new JLabel(new ImageIcon(&quot;itheima-array2\\images\\&quot;+(i+13)+&quot;.png&quot;));</span></span><br><span class="line"><span class="comment">//            jLabel.setBounds(i*90,270,90,90);</span></span><br><span class="line"><span class="comment">//            jf.add(jLabel);</span></span><br><span class="line"><span class="comment">//        &#125;</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//定义图片编号的数组</span></span><br><span class="line">        <span class="type">int</span>[][] datas = &#123;</span><br><span class="line">                &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>&#125;,</span><br><span class="line">                &#123;<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>&#125;,</span><br><span class="line">                &#123;<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>&#125;,</span><br><span class="line">                &#123;<span class="number">13</span>,<span class="number">14</span>,<span class="number">15</span>,<span class="number">16</span>&#125;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; datas.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; datas[i].length; j++) &#123;</span><br><span class="line">                <span class="comment">//datas[i][j]</span></span><br><span class="line">                <span class="type">JLabel</span> <span class="variable">jLabel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-array2\\images\\&quot;</span>+datas[i][j]+<span class="string">&quot;.png&quot;</span>));</span><br><span class="line">                jLabel.setBounds(j*<span class="number">90</span>,i*<span class="number">90</span>,<span class="number">90</span>,<span class="number">90</span>);</span><br><span class="line">                jf.add(jLabel);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>在代码的讲解过程中，我们先一张张的展示图片，后面通过二维数组的方式改进了图片的存储，最终优化了图片展示。</p>
<p>好了，关于案例图片展示我们就先讲到这里</p>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>
<h3 id="案例8图片打乱">2.8 案例8(图片打乱)</h3>
<p>来，继续啊，下面我们来做一个案例图片打乱。</p>
<p>刚才我们讲解了图片展示的案例，现在呢，要在图片展示的基础上，把图片打乱，我们可以看一下打乱后的效果，其实就是对二维数组中的图片的编号打乱，然后再显示出来就可以了。</p>
<p>下面呢，我们到IDEA中一起去实现一下：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">import</span> javax.swing.*;</span><br><span class="line"><span class="keyword">import</span> java.util.Random;</span><br><span class="line"></span><br><span class="line"><span class="comment">/*</span></span><br><span class="line"><span class="comment">    图片打乱</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">class</span> <span class="title class_">ArrayTest04</span> &#123;</span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title function_">main</span><span class="params">(String[] args)</span> &#123;</span><br><span class="line">        <span class="comment">//创建窗体对象</span></span><br><span class="line">        <span class="type">JFrame</span> <span class="variable">jf</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JFrame</span>();</span><br><span class="line"></span><br><span class="line">        jf.setTitle(<span class="string">&quot;图片打乱&quot;</span>);</span><br><span class="line">        jf.setSize(<span class="number">380</span>, <span class="number">400</span>);</span><br><span class="line">        jf.setDefaultCloseOperation(<span class="number">3</span>);</span><br><span class="line">        jf.setLocationRelativeTo(<span class="literal">null</span>);</span><br><span class="line">        jf.setAlwaysOnTop(<span class="literal">true</span>);</span><br><span class="line">        jf.setLayout(<span class="literal">null</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//图片打乱</span></span><br><span class="line">        <span class="type">int</span>[][] datas = &#123;</span><br><span class="line">                &#123;<span class="number">1</span>,<span class="number">2</span>,<span class="number">3</span>,<span class="number">4</span>&#125;,</span><br><span class="line">                &#123;<span class="number">5</span>,<span class="number">6</span>,<span class="number">7</span>,<span class="number">8</span>&#125;,</span><br><span class="line">                &#123;<span class="number">9</span>,<span class="number">10</span>,<span class="number">11</span>,<span class="number">12</span>&#125;,</span><br><span class="line">                &#123;<span class="number">13</span>,<span class="number">14</span>,<span class="number">15</span>,<span class="number">16</span>&#125;</span><br><span class="line">        &#125;;</span><br><span class="line"></span><br><span class="line">        <span class="type">Random</span> <span class="variable">r</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">Random</span>();</span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; datas.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; datas[i].length; j++) &#123;</span><br><span class="line">                <span class="type">int</span> <span class="variable">x</span> <span class="operator">=</span> r.nextInt(datas.length);</span><br><span class="line">                <span class="type">int</span> <span class="variable">y</span> <span class="operator">=</span> r.nextInt(datas[x].length);</span><br><span class="line"></span><br><span class="line">                <span class="type">int</span> <span class="variable">temp</span> <span class="operator">=</span> datas[i][j];</span><br><span class="line">                datas[i][j] = datas[x][y];</span><br><span class="line">                datas[x][y] = temp;</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">i</span> <span class="operator">=</span> <span class="number">0</span>; i &lt; datas.length; i++) &#123;</span><br><span class="line">            <span class="keyword">for</span> (<span class="type">int</span> <span class="variable">j</span> <span class="operator">=</span> <span class="number">0</span>; j &lt; datas[i].length; j++) &#123;</span><br><span class="line">                <span class="type">JLabel</span> <span class="variable">jLabel</span> <span class="operator">=</span> <span class="keyword">new</span> <span class="title class_">JLabel</span>(<span class="keyword">new</span> <span class="title class_">ImageIcon</span>(<span class="string">&quot;itheima-array2\\images\\&quot;</span>+datas[i][j]+<span class="string">&quot;.png&quot;</span>));</span><br><span class="line">                jLabel.setBounds(j*<span class="number">90</span>,i*<span class="number">90</span>,<span class="number">90</span>,<span class="number">90</span>);</span><br><span class="line">                jf.add(jLabel);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;</span><br><span class="line"></span><br><span class="line">        jf.setVisible(<span class="literal">true</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>讲解完毕后，大家赶快动手练习一下吧。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="reward-container">
  <div>请我一杯咖啡吧！</div>
  <button>
    赞赏
  </button>
  <div class="post-reward">
      <div>
        <img src="/images/wChat.png" alt="Ethan Liu 微信">
        <span>微信</span>
      </div>

  </div>
</div>

          

<div class="post-copyright">
<ul>
  <li class="post-copyright-author">
      <strong>本文作者： </strong>Ethan Liu
  </li>
  <li class="post-copyright-link">
      <strong>本文链接：</strong>
      <a href="http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/" title="JavaSE基础——Day08 JavaSE基础——数组">http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE基础/day08数组/</a>
  </li>
  <li class="post-copyright-license">
      <strong>版权声明： </strong>本博客所有文章除特别声明外，均采用 <span class="exturl" data-url="aHR0cHM6Ly9jcmVhdGl2ZWNvbW1vbnMub3JnL2xpY2Vuc2VzL2J5LW5jLXNhLzQuMC8="><i class="fab fa-fw fa-creative-commons"></i>BY-NC-SA</span> 许可协议。转载请注明出处！
  </li>
</ul>
</div>


          <div class="post-tags">
              <a href="/tags/Java/" rel="tag"><i class="fa fa-tag"></i> Java</a>
              <a href="/tags/Java%E5%9F%BA%E7%A1%80/" rel="tag"><i class="fa fa-tag"></i> Java基础</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2024/03/21/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day07%E5%B8%B8%E7%94%A8API(2)/" rel="prev" title="JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF">
                  <i class="fa fa-angle-left"></i> JavaSE基础——Day07 JavaSE基础——常用API:String、Integer、Date、SDF
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2024/03/24/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day09%E5%8A%A8%E6%BC%AB%E7%BE%8E%E5%A5%B3%E6%8B%BC%E5%9B%BE/" rel="next" title="JavaSE基础——Day09 JavaSE基础——综合案例动漫美女拼图">
                  JavaSE基础——Day09 JavaSE基础——综合案例动漫美女拼图 <i class="fa fa-angle-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments" id="SOHUCS" sid="1ebf8e539b79390756bba9475ddb75d7"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">

  <div class="copyright">
    &copy; 2023-2024 – 
    <span itemprop="copyrightYear">2024</span>
    <span class="with-love">
      <i class="fa fa-heart"></i>
    </span>
    <span class="author" itemprop="copyrightHolder">Is Ethan's</span>
  </div>
<div class="wordcount">
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-chart-line"></i>
    </span>
    <span title="站点总字数">113k</span>
  </span>
  <span class="post-meta-item">
    <span class="post-meta-item-icon">
      <i class="fa fa-coffee"></i>
    </span>
    <span title="站点阅读时长">6:51</span>
  </span>
</div>
<div class="busuanzi-count">
    <span class="post-meta-item" id="busuanzi_container_site_uv">
      <span class="post-meta-item-icon">
        <i class="fa fa-user"></i>
      </span>
      <span class="site-uv" title="总访客量">
        <span id="busuanzi_value_site_uv"></span>
      </span>
    </span>
    <span class="post-meta-item" id="busuanzi_container_site_pv">
      <span class="post-meta-item-icon">
        <i class="fa fa-eye"></i>
      </span>
      <span class="site-pv" title="总访问量">
        <span id="busuanzi_value_site_pv"></span>
      </span>
    </span>
</div>

    </div>
  </footer>

  
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>
  <div class="reading-progress-bar"></div>
  <a role="button" class="book-mark-link book-mark-link-fixed"></a>

  <span class="exturl github-corner" data-url="aHR0cHM6Ly9naXRodWIuY29tL0V0aGFuTGl1Ni8=" title="在 GitHub 上关注我" aria-label="在 GitHub 上关注我"><svg width="80" height="80" viewBox="0 0 250 250" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></span>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/next-theme-pjax/0.6.0/pjax.min.js" integrity="sha256-vxLn1tSKWD4dqbMRyv940UYw4sXgMtYcK6reefzZrao=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/fancyapps-ui/5.0.33/fancybox/fancybox.umd.js" integrity="sha256-+2+qOqR8CKoHh/AsVR9k2qaDBKWjYNC2nozhYmv5j9k=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/medium-zoom/1.1.0/medium-zoom.min.js" integrity="sha256-ZgMyDAIYDYGxbcpJcfUnYwNevG/xi9OHKaR/8GK+jWc=" crossorigin="anonymous"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/lozad.js/1.16.0/lozad.min.js" integrity="sha256-mOFREFhqmHeQbXpK2lp4nA3qooVgACfh88fpJftLBbc=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/next-boot.js"></script><script src="/js/bookmark.js"></script><script src="/js/pjax.js"></script>

  <script src="https://cdnjs.cloudflare.com/ajax/libs/hexo-generator-searchdb/1.4.1/search.js" integrity="sha256-1kfA5uHPf65M5cphT2dvymhkuyHPQp5A53EGZOnOLmc=" crossorigin="anonymous"></script>
<script src="/js/third-party/search/local-search.js"></script>

  <script class="next-config" data-name="pdf" type="application/json">{"object_url":{"url":"https://cdnjs.cloudflare.com/ajax/libs/pdfobject/2.2.12/pdfobject.min.js","integrity":"sha256-g2xji1rlE3KsGVClvuxTbcR0Kn2+wtQADSff2Tbb4zA="},"url":"/lib/pdf/web/viewer.html"}</script>
  <script src="/js/third-party/tags/pdf.js"></script>



  <script src="/js/third-party/fancybox.js"></script>



  
  <script data-pjax async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"all","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>


  <script src="https://cdnjs.cloudflare.com/ajax/libs/quicklink/2.3.0/quicklink.umd.js" integrity="sha256-yvJQOINiH9fWemHn0vCA5lsHWJaHs6/ZmO+1Ft04SvM=" crossorigin="anonymous"></script>
  <script class="next-config" data-name="quicklink" type="application/json">{"enable":true,"home":true,"archive":true,"delay":true,"timeout":3000,"priority":true,"url":"http://ethanliu6.github.io/2024/03/22/JavaNotes/JavaSE%E5%9F%BA%E7%A1%80/day08%E6%95%B0%E7%BB%84/"}</script>
  <script src="/js/third-party/quicklink.js"></script>
<script class="next-config" data-name="changyan" type="application/json">{"enable":true,"appid":"cyxf6tytI","appkey":"90987f30c4b5248baad5a04c7a6abfe1","count":true}</script>
<script src="/js/third-party/comments/changyan.js"></script>

</body>
</html>
